package com.aidex.biz.controller;

import java.util.List;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.MessageInfo;
import com.aidex.biz.service.MessageInfoService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 系统消息Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-03-06
 */
@RestController
@RequestMapping("/biz/bizMessageInfo")
public class MessageInfoController extends BaseController {
    @Autowired
    private MessageInfoService messageInfoService;

    /**
     * 查询系统消息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMessageInfo:list')")
    @GetMapping("/list")
    public R<PageInfo> list(MessageInfo messageInfo, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            messageInfo.setCompanyId(null);
        } else {
            messageInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
            messageInfo.setUserId(SecurityUtils.getLoginUser().getUserId());
        }
        messageInfo.setPage(new PageDomain(request, response));
        return R.data(messageInfoService.findPage(messageInfo));
    }

    /**
     * 获取系统消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMessageInfo:query')")
    @GetMapping(value = "/{id}")
    public R<MessageInfo> detail(@PathVariable("id") String id) {
        return R.data(messageInfoService.get(id));
    }

    /**
     * 新增系统消息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMessageInfo:add')")
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated MessageInfo messageInfo) {
        return R.status(messageInfoService.save(messageInfo));
    }

    /**
     * 修改系统消息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMessageInfo:edit')")
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated MessageInfo messageInfo) {
        return R.status(messageInfoService.save(messageInfo));
    }


    /**
     * 删除系统消息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMessageInfo:remove')")
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(messageInfoService.deleteMessageInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMessageInfo:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(messageInfoService.findMaxSort(new MessageInfo()));
    }

    /**
     * 导出系统消息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMessageInfo:export')")
    @Log(title = "系统消息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(MessageInfo messageInfo) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            messageInfo.setCompanyId(null);
        } else {
            messageInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<MessageInfo> list = messageInfoService.findList(messageInfo);
        ExcelUtil<MessageInfo> util = new ExcelUtil<MessageInfo>(MessageInfo.class);
        return util.exportExcel(list, "系统消息数据");
    }


    /**
     * 修改系统消息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizMessageInfo:edit')")
    @PutMapping("/updateIsReadMulti")
    public R updateIsReadMulti(@RequestBody @Validated MessageInfo messageInfo) {
        messageInfo.setIsRead(1);
        return R.status(messageInfoService.updateIsReadMulti(messageInfo));
    }

}
